package com.zzxz.practicaltrainingsys.service.impl;

import com.zzxz.practicaltrainingsys.entity.ChatRecord;
import com.zzxz.practicaltrainingsys.entity.Message;
import com.zzxz.practicaltrainingsys.mapper.ChatRecordMapper;
import com.zzxz.practicaltrainingsys.service.ChatRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;

@Service
public class ChatRecordServiceImpl implements ChatRecordService {

    @Autowired
    private ChatRecordMapper chatRecordMapper;

    @Override
    public boolean saveChatRecord(Message message, String chatType, String inviteCode) {
        try {
            if (message == null || !StringUtils.hasText(message.getContent())) {
                return false;
            }

            ChatRecord chatRecord = new ChatRecord(
                    message.getFrom(),
                    message.getUserInfo() != null ? message.getUserInfo().getRealName() : null,
                    message.getUserInfo() != null ? message.getUserInfo().getType() : null,
                    message.getContent(),
                    chatType,
                    inviteCode
            );

            return chatRecordMapper.insertChatRecord(chatRecord) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<ChatRecord> getChatRecords(String chatType, String inviteCode, int page, int size) {
        try {
            int offset = (page - 1) * size;
            List<ChatRecord> records = chatRecordMapper.findChatRecords(chatType, inviteCode, offset, size);
            // 返回时间正序（最早的在前面）
            Collections.reverse(records);
            return records;
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    @Override
    public List<ChatRecord> getRecentChatRecords(String chatType, String inviteCode, int limit) {
        try {
            List<ChatRecord> records = chatRecordMapper.findRecentChatRecords(chatType, inviteCode, limit);
            // 返回时间正序（最早的在前面）
            Collections.reverse(records);
            return records;
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    @Override
    public List<ChatRecord> getUserChatRecords(String username, int page, int size) {
        try {
            int offset = (page - 1) * size;
            return chatRecordMapper.findUserChatRecords(username, offset, size);
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    @Override
    public int getChatRecordCount(String chatType, String inviteCode) {
        try {
            return chatRecordMapper.countChatRecords(chatType, inviteCode);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int cleanOldChatRecords(int daysAgo) {
        try {
            LocalDateTime beforeTime = LocalDateTime.now().minusDays(daysAgo);
            String beforeTimeStr = beforeTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return chatRecordMapper.deleteChatRecordsBefore(beforeTimeStr);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
